/*
 *  Copyright:  (C) 2018 LingYun IoT System Studio
 *              All rights reserved.
 *
 *  File name:  daemon.c
 *Description:  Test daemon() and syslog().
 *
 *    Version:  1.1
 *     Author:  Guo Wenxue <guowenxue@gmail.com>
 *		        Copied by Zhu Tianyin <zhuty_cn@163.com>
 *  ChangeLog:  1.0  Release initial version in 2018.
 *              1.1  Copy and change some details on "01/29/2021".
 */
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <syslog.h>
#include <libgen.h>

int main(int argc, char **argv)
{
    char *progname = basename(argv[0]);

    if( daemon(0, 0) < 0 )
    {
        printf("Program daemon() failure：%s\n",strerror(errno));
        return -1;
    }
    openlog("daemon", LOG_CONS | LOG_PID, 0);
    syslog(LOG_NOTICE, "Program '%s' start running\n", progname);
    syslog(LOG_WARNING, "Program '%s' running with a warning message\n", progname);
    syslog(LOG_EMERG, "Program '%s' running with a emergency message\n", progname);

    while(1)
    {
        ;                               //Do something here.
    }

    syslog(LOG_NOTICE, "Program '%s' stop running\n", progname);
    closelog();

    return 0;
}

